﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using ModelPredictPro.WindowForms.AppCodes;
using ModelPredictPro.Predict.SVM;

namespace ModelPredictPro.WindowForms.UserControls {
    public partial class UCTrainTask : SVMUserControl, ISVMTaskHost {
        public UCTrainTask() {
            InitializeComponent();
        }

        private void UCTrainTask_Load(object sender, EventArgs e) {
            txtParamOutputFile.OpenFileButton.Enabled = ComponentFactory.Krypton.Toolkit.ButtonEnabled.False;
        }


        private void chbTrainValidate_CheckedChanged(object sender, EventArgs e) {
            txtTrainValidateFile.Visible = chbTrainValidate.Checked;
        }


        private void btnAddTask_Click(object sender, EventArgs e) {
            bool validate = true;
            validate = txtTrainDatasetFile.CheckFileExist();
            validate = txtParamOutputFile.CheckFileDirectoryExist() && validate;
            if (chbTrainValidate.Checked)
                validate = txtTrainValidateFile.CheckFileExist() && validate;
            if (validate)
                AddTask(CreateTask());
        }

        private void btnBulkAdd_Click(object sender, EventArgs e) {
            string[] files;
            bool batch = txtTrainDatasetFile.CheckMultiFileExist(out files);
            if (!batch) return;
            batch = txtParamOutputFile.CheckMultiFileTemplate() && batch;
            if (chbTrainValidate.Checked)
                batch = txtTrainValidateFile.CheckMultiFileTemplate() && batch;
            if (!batch) return;

            if (MessageBox.Show(string.Format("Add {0} tasks, continue?", files.Length), "Query", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) {
                
                SVMFactory factory = UCSVMSetting.SingleInstance.CreateSVMFactory();
                var parma = factory.CreatePredictParam();
                
                foreach (string item in files) {
                    string paramOutputFile = txtParamOutputFile.GetRealFilePath(item);
                    string trainValidateFile = null;
                    if (chbTrainValidate.Checked)
                        trainValidateFile = txtTrainValidateFile.GetRealFilePath(item);
                    AddTask(new TrainTask(parma, item, trainValidateFile, paramOutputFile));
                }
            }
        }

        public override ITask CreateTask() {
            ITask task = null;
            SVMFactory factory = UCSVMSetting.SingleInstance.CreateSVMFactory();
            var parma = factory.CreatePredictParam();
            if (chbTrainValidate.Checked)
                task = new TrainTask(parma, TrainSetFile, TrainValidateFile, ParamOutFile);
            else
                task = new TrainTask(parma, TrainSetFile, ParamOutFile);
            return task;         
        }

        #region abstract field

        public override string TrainSetFile {
            get {
                return txtTrainDatasetFile.Text;
            }
            set {
                txtTrainDatasetFile.Text = value;
            }
        }

        public override string TrainValidateFile {
            get {
                return txtTrainValidateFile.Text;
            }
            set {
                txtTrainValidateFile.Text = value;
            }
        }

        public override string ParamOutFile {
            get {
                return txtParamOutputFile.Text;
            }
            set {
                txtParamOutputFile.Text = value;
            }
        }

        #endregion


    }
}
